/*
 * PriceComparisonView.java
 */

package pricecomparison;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jdesktop.application.ApplicationContext;
import org.jdesktop.application.Task;
import org.jdesktop.application.TaskService;
import pricecomparison.websitereader.apexlamps.AL_HomePageReader;
import pricecomparison.websitereader.discountmerchant.DM_HomePageReader;
import pricecomparison.websitereader.projectorquest.PQ_HomePageReader;
import pricecomparison.websitereader.purelandreader.PL_HomePageReader;

/**
 * The application's main frame.
 */
public class PriceComparisonView extends FrameView {

    public PriceComparisonView(SingleFrameApplication app) {
        super(app);

        initComponents();
        
        stop.setEnabled(false);
        
        try {
            readBrands();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = PriceComparisonApp.getApplication().getMainFrame();
            aboutBox = new PriceComparisonAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        PriceComparisonApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        manufacturerList = new javax.swing.JComboBox();
        searchButton = new javax.swing.JButton();
        countLabel = new javax.swing.JLabel();
        count = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        nowSearching = new javax.swing.JLabel();
        stop = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        optionsMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();

        mainPanel.setName("mainPanel"); // NOI18N

        manufacturerList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "3D Perception", "3M", "AK", "Acer", "ADI", "Advance Electronic", "Advent", "Akai", "Ampro", "Anders Kern", "Ansi", "Anthem", "APO", "Apollo", "Apti", "Ask", "AV Plus", "AV Vision", "Avio", "Barco", "BenQ", "Bluesky", "Bonama", "Boxlight", "Canon", "Casio", "Chatani Enrich", "Chisholm", "Christie", "Cinego", "CineVersum", "Clarity", "Claxam", "Compaq", "CTX", "Davis", "Dell", "Delta", "DepthQ", "Digital Projection", "Dream Vision", "Dukane", "DWIN", "Dynamica", "Eiki", "Eizo", "Electrohome", "Elite Video", "Elmo", "Elux", "Epson", "Everest", "Faqtor", "Faroudja", "Flashtube", "FoxConn/Premier", "Fujitsu", "Fujitsu/Siemens", "Gateway", "GE", "Geha", "Gemstar", "Halogen Lamp Bulb", "HP", "Hisense", "Hitachi", "Hughes/JVC", "Hustem", "IBM", "Iiyama", "Infocus", "IQI", "JVC", "Kindermann", "Knoll", "Kodak", "Kolin", "Lasergraphics", "Lenovo", "LG", "LG/Zenith", "Liesegang", "Lightware", "Loewe Articos", "Lumax", "Lumens", "Luxeon", "Maginon", "Magnavox", "Marantz", "Matrix", "Matsushita", "MCSi", "Mediavision", "Medion", "Medium", "MegaPower", "Metavision", "Mitsubishi", "MLX", "Multivision", "Mustek", "Navitar", "NEC", "Nexgen", "Nobo", "Nview", "OHP", "Olevia", "Olympus", "Optoma", "Osram", "Other", "Packard Bell", "Panasonic", "Pelco", "Phillips", "Phoenix", "Pioneer", "Planar", "Plus", "Polaroid", "Premeir", "ProjectionDesign", "Projector Europe", "Prokia", "Promethean", "Proxima", "Pureland", "RCA", "Reflecta", "Relisys", "Rollei", "Roverlight", "Runco", "Sagem", "Sahara", "Samsung", "Sanyo", "Sauerwein", "Saville", "Schneider AG", "Scott", "Selco", "Sharp", "Sim2", "Skyworth", "Smartboard", "Sony", "Studio Experience", "TA", "Taxan", "Telex", "Themescene", "Thomson", "Toshiba", "Triumph-Adler", "UMAX", "Ushio", "Utax", "Vertex", "Video7", "Videor", "Vidikron", "Viewsonic", "Viore", "Vision", "Vivitar", "Vivitek", "Vizio", "Welch-Allyn", "Wildcat", "Xerox", "Yamaha", "Yokogawa", "Zenith" }));
        manufacturerList.setName("manufacturersList"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pricecomparison.PriceComparisonApp.class).getContext().getResourceMap(PriceComparisonView.class);
        searchButton.setText(resourceMap.getString("searchButton.text")); // NOI18N
        searchButton.setToolTipText(resourceMap.getString("searchButton.toolTipText")); // NOI18N
        searchButton.setActionCommand(resourceMap.getString("searchButton.actionCommand")); // NOI18N
        searchButton.setName("searchButton"); // NOI18N
        searchButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                beginSearch(evt);
            }
        });

        countLabel.setText(resourceMap.getString("countLabel.text")); // NOI18N
        countLabel.setName("countLabel"); // NOI18N

        count.setText(resourceMap.getString("count.text")); // NOI18N
        count.setName("count"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        nowSearching.setText(resourceMap.getString("nowSearching.text")); // NOI18N
        nowSearching.setName("nowSearching"); // NOI18N

        stop.setText(resourceMap.getString("stop.text")); // NOI18N
        stop.setName("stop"); // NOI18N
        stop.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                stopMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(manufacturerList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(searchButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(stop))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(countLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(count))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nowSearching)))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(manufacturerList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchButton)
                    .addComponent(stop))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(countLabel)
                    .addComponent(count))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nowSearching))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pricecomparison.PriceComparisonApp.class).getContext().getActionMap(PriceComparisonView.class, this);
        optionsMenuItem.setAction(actionMap.get("showOptions")); // NOI18N
        optionsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        optionsMenuItem.setText(resourceMap.getString("optionsMenuItem.text")); // NOI18N
        optionsMenuItem.setName("optionsMenuItem"); // NOI18N
        fileMenu.add(optionsMenuItem);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

private void beginSearch(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_beginSearch
    count.setText("0");
    stop.setEnabled(true);
    searchButton.setEnabled(false);
    manufacturerList.setEnabled(false);
    
    task = new SearchTask(org.jdesktop.application.Application.getInstance());
    ApplicationContext C = getApplication().getContext();
    TaskMonitor M = C.getTaskMonitor();
    TaskService S = C.getTaskService();
    S.execute(task);
    M.setForegroundTask(task);
}//GEN-LAST:event_beginSearch

private void stopMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_stopMouseClicked
    if (task.cancel(true) ) {
        manufacturerList.setEnabled(true);
        stop.setEnabled(false);
        searchButton.setEnabled(true);
        nowSearching.setText("");
    }
    try {
        excelWriter.workbook.write();
        excelWriter.workbook.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}//GEN-LAST:event_stopMouseClicked

private void readBrands() {
    File file = new File(BRAND_FILE);
    
    try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = reader.readLine();
        int r = 0;
        
        while ((line = reader.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line,",");
            int c = 0;
            while (st.hasMoreTokens()) {
                brands[r][c] = st.nextToken();
                c++;
            }
            r++;
        }
        
        reader.close();
    } catch (java.io.FileNotFoundException e) {
        JOptionPane.showMessageDialog(null, "Cannot initilize brands dropdown with " + BRAND_FILE + ": File not found.");
    } catch (java.io.IOException e) {
        JOptionPane.showMessageDialog(null, "Error encountered with WebsiteReader from readBrands: " + e.getMessage());
    }
}

private class SearchTask extends Task<Void, Void> {
    public SearchTask(org.jdesktop.application.Application app) {
        super(app);
    }
    
    @Override
    protected Void doInBackground() {
        String[] brand = brands[manufacturerList.getSelectedIndex()];
        boolean success = true;
        
        // Start an excel worksheet for manufacturer
        try {
            excelWriter = new ExcelWriter(brand[0].replace("/", ""));
        } catch (java.io.IOException e) {
            JOptionPane.showMessageDialog(null, "Error while opening excel writer: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

        // Search Pureland
        boolean hasStarted = false;
        if (optionsBox.pureland.isSelected()) {
            nowSearching.setText("Pureland");
            PL_HomePageReader pl_reader;

            for (int i = 1; i < 4; i++) {
                if (!brand[i].equals("-")) {
                    pl_reader = new PL_HomePageReader();
                    try {
                        pl_reader.beginSearch(brand[i], !hasStarted, count);
                    } catch (jxl.write.WriteException e) {
                        JOptionPane.showMessageDialog(null, "WriteException during search of Pureland: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                        success = false;
                    } catch (java.net.MalformedURLException e) {
                        JOptionPane.showMessageDialog(null, "MaulformedURLException during search of Pureland: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                        success = false;
                    } catch (java.io.IOException e) {
                        JOptionPane.showMessageDialog(null, "IOException during search of Pureland: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                        success = false;
                }
                    hasStarted = true;
                }
            }
        }

        // Search ProjectorQuest Projector Lamps
        if (optionsBox.projectorQuest.isSelected()) {
            nowSearching.setText("Projector Quest Projector Lamps");
            PQ_HomePageReader pq_reader = new PQ_HomePageReader();
            hasStarted = false;
            if (!brand[4].equals("-")) {
                try {
                    pq_reader.beginSearch(brand[4], true, count);
                } catch (jxl.write.WriteException e) {
                    JOptionPane.showMessageDialog(null, "WriteException during search of Discount-Merchant Projector Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.net.MalformedURLException e) {
                    JOptionPane.showMessageDialog(null, "MaulformedURLException during search of Discount-Merchant Projector Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.io.IOException e) {
                    JOptionPane.showMessageDialog(null, "IOException during search of Discount-Merchant Projector Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                }
                hasStarted = true;
            }

            // Search ProjectorQuest TV Lamps
            nowSearching.setText("Projector Quest TV Lamps");
            pq_reader.URL = "http://www.projectorquest.com/projection-tv-lamps.html";
            if (!brand[5].equals("-")) {
                try {
                    pq_reader.beginSearch(brand[5], !hasStarted, count);
                } catch (jxl.write.WriteException e) {
                    JOptionPane.showMessageDialog(null, "WriteException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.net.MalformedURLException e) {
                    JOptionPane.showMessageDialog(null, "MaulformedURLException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.io.IOException e) {
                    JOptionPane.showMessageDialog(null, "IOException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                }
            }
        }

        // Search Discount-Merchant TV Lamps
        if (optionsBox.discountMerchant.isSelected()) {
            nowSearching.setText("Discount Merchant TV Lamps");
            DM_HomePageReader dm_reader = new DM_HomePageReader();
            hasStarted = false;
            if (!brand[6].equals("-")) {
                try {
                    dm_reader.beginSearch(brand[6], true, count);
                } catch (jxl.write.WriteException e) {
                    JOptionPane.showMessageDialog(null, "WriteException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.net.MalformedURLException e) {
                    JOptionPane.showMessageDialog(null, "MaulformedURLException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.io.IOException e) {
                    JOptionPane.showMessageDialog(null, "IOException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                }
                hasStarted = true;
            }

            // Search Discount-Merchant Projector Lamps
            nowSearching.setText("Discount-Merchant Projector Lamps");
            if (!brand[7].equals("-")) {
                try {
                    dm_reader.beginSearch(brand[7], !hasStarted, count);
                } catch (jxl.write.WriteException e) {
                    JOptionPane.showMessageDialog(null, "WriteException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.net.MalformedURLException e) {
                    JOptionPane.showMessageDialog(null, "MaulformedURLException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.io.IOException e) {
                    JOptionPane.showMessageDialog(null, "IOException during search of Discount-Merchant TV Lamps: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                }
            }
        }

        // Search Apex Lamps Video Wall
        if (optionsBox.apex.isSelected()) {
            nowSearching.setText("Apex Lamps Video Wall");
            AL_HomePageReader al_reader = new AL_HomePageReader();
            hasStarted = false;
            if (!brand[8].equals("-")) {
                try {
                    al_reader.beginSearch(brand[8], true, count);
                } catch (jxl.write.WriteException e) {
                    JOptionPane.showMessageDialog(null, "WriteException during search of Apex Lamps Video Wall: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.net.MalformedURLException e) {
                    JOptionPane.showMessageDialog(null, "MaulformedURLException during search of Apex Lamps Video Wall: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.io.IOException e) {
                    JOptionPane.showMessageDialog(null, "IOException during search of Apex Lamps Video Wall: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                }
                hasStarted = true;
            }

            // Search Apex Lamps Front End Projector
            nowSearching.setText("Apex Lamps Front End Projector");
            al_reader.URL = "http://apexlamps.com/index.php?main_page=index&cPath=1_2&sort=20a&filter_id=";
            if (!brand[9].equals("-")) {
                try {
                    al_reader.beginSearch(brand[9], !hasStarted, count);
                } catch (jxl.write.WriteException e) {
                    JOptionPane.showMessageDialog(null, "WriteException during search of Apex Lamps Front End Projector: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.net.MalformedURLException e) {
                    JOptionPane.showMessageDialog(null, "MaulformedURLException during search of Apex Lamps Front End Projector: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.io.IOException e) {
                    JOptionPane.showMessageDialog(null, "IOException during search of Apex Lamps Front End Projector: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                }
                hasStarted = true;
            }

            // Search Apex Lamps Rear Projection TV
            nowSearching.setText("Apex Lamps Rear Projection TV");
            al_reader.URL = "http://apexlamps.com/index.php?main_page=index&cPath=1_3&sort=20a&filter_id=";
            if (!brand[10].equals("-")) {
                try {
                    al_reader.beginSearch(brand[10], !hasStarted, count);
                } catch (jxl.write.WriteException e) {
                    JOptionPane.showMessageDialog(null, "WriteException during search of Apex Lamps Video Wall: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.net.MalformedURLException e) {
                    JOptionPane.showMessageDialog(null, "MaulformedURLException during search of Apex Lamps Video Wall: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                } catch (java.io.IOException e) {
                    JOptionPane.showMessageDialog(null, "IOException during search of Apex Lamps Video Wall: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    success = false;
                }
            }
        }

        // Write and close excel
        if (excelWriter.workbook.getNumberOfSheets() > 0) {
            try {
                excelWriter.workbook.write();
                excelWriter.workbook.close();
            } catch (java.io.IOException e) {
                JOptionPane.showMessageDialog(null, "IOException during closing of excel writer: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                success = false;
            } catch (jxl.write.WriteException e) {
                JOptionPane.showMessageDialog(null, "WriteException during closing of excel writer: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                success = false;
            }
        }
        
        if (success)
            JOptionPane.showMessageDialog(null, "Finished " + manufacturerList.getSelectedItem() + "!", "Finished", JOptionPane.INFORMATION_MESSAGE);
        manufacturerList.setEnabled(true);
        stop.setEnabled(false);
        nowSearching.setText("");
        searchButton.setEnabled(true);
        return null;
    }
}

@Action
public void showOptions() {
    if (optionsBox == null) {
        JFrame mainFrame = PriceComparisonApp.getApplication().getMainFrame();
        optionsBox = new PriceComparisonOptionsBox(mainFrame);
        optionsBox.setLocationRelativeTo(mainFrame);
    }
    PriceComparisonApp.getApplication().show(optionsBox);
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    public javax.swing.JLabel count;
    private javax.swing.JLabel countLabel;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JComboBox manufacturerList;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel nowSearching;
    private javax.swing.JMenuItem optionsMenuItem;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JButton searchButton;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JButton stop;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
    private PriceComparisonOptionsBox optionsBox;
    
    public static ExcelWriter excelWriter;
    String[][] brands = new String[176][11];
    final String BRAND_FILE = "brands.csv";
    SearchTask task;
}
